package com.rongxun.hbwallet.ui.activities;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongxun.base.MvpActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.model.bean.ImageCode;
import com.rongxun.hbwallet.mvp.contract.ICodeLogin;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.hbwallet.mvp.presenter.ICodeLoginPresenter;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.AppUtils;
import com.rongxun.utils.EventConf;
import com.rongxun.utils.EventUtil;
import com.rongxun.utils.MyToast;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.widget.LoadingDialog;
import com.rongxun.widget.TimeTexView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/13 9:47
 * 描述
 */
public class CodeLoginActivity extends MvpActivity<ICodeLogin.IView,ICodeLoginPresenter> implements ICodeLogin.IView {
    @BindView(R.id.ed_codelogin_phone)
    EditText  ed_phone;
    @BindView(R.id.codelogin_et_img_code)
    EditText  ed_imgcode;
    @BindView(R.id.codelogin_mobile_code)
    EditText  ed_mobile_code;
    @BindView(R.id.toolbar_title)
    TextView  mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar   mToolbar;
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    private Dialog mLoadingDialog;
    @BindView(R.id.codelogin_get_code)
    TimeTexView get_code;
    @BindView(R.id.codelogin_img_code)
    ImageView   img_code;
    private String captchaId;
    private Bitmap bitmap;
    @Override
    protected void init() {
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        mToolbarTitle.setText(getString(R.string.phone_code_login));
        mToolbarBack.setVisibility(View.VISIBLE);
        mLoadingDialog = LoadingDialog.createLoadingDialog(this, "加载中", false);
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        get_code.setPaintFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线
        get_code.getPaint().setAntiAlias(true);//抗锯齿
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_code_login;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading(boolean show) {
        if (show) {
            mLoadingDialog.show();
        } else {
            mLoadingDialog.cancel();
        }
    }

    @Override
    public void showError(String msg, String errorCode) {
        mLoadingDialog.cancel();
        MyToast.showToast(this,msg);
    }

    @Override
    public void checkPermissionsSuccessful(String... permissions) {

    }

    @Override
    public void release() {

    }

    @NonNull
    @Override
    public ICodeLoginPresenter createPresenter() {
        return new ICodeLoginPresenter(this, new ApiModel(this));
    }

    @Override
    public void showLogin() {
        EventUtil.postInfoEvent(EventConf.USER_CHANGE,0);
        ActivityUtils.startActivityWithAnimotion(this,new Intent(this,MainActivity.class));
    }

    @Override
    public void showCodeimage(ImageCode code) {
        if(code!=null){
          captchaId=code.getCaptcha().getId();
           bitmap= AppUtils.stringtoBitmap(code.getCaptcha().getCode());
          img_code.setImageBitmap(bitmap);
        }
    }

    @Override
    public void codeTextTiTimte() {
        get_code.startTime();
    }

    @OnClick({R.id.toolbar_back,R.id.codelogin_img_code,R.id.codelogin_get_code,R.id.but_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.codelogin_img_code:
                 getPresenter().getCode();
                break;
            case  R.id.toolbar_back:
                onBackPressed();
                break;
            case R.id.codelogin_get_code:
                String country=getContext().getString(R.string.current_country);
                getPresenter().getMobileNumber(ed_imgcode.getText().toString(),captchaId,country,ed_phone.getText().toString());
                break;
            case R.id.but_login:
                getPresenter().login(ed_phone.getText().toString(),"captcha",ed_mobile_code.getText().toString());
                break;
        }
    }
}
